package com.shd.edanpei.presenter;

import android.text.TextUtils;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.PicBean;
import com.shd.edanpei.bean.UpFileBean;
import com.shd.edanpei.presenter.contract.UpFileContract;
import com.shd.edanpei.enums.PicEnum;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by mc on 2018/4/4.
 * yang-wen-wen
 */

public class UpFilePresenter extends BasePresenterImpl<UpFileContract.UpFileIView>
        implements UpFileContract.UpFileIPresenter {

    private PicBean picBean = new PicBean();

    public UpFilePresenter(UpFileContract.UpFileIView mView) {
        super(mView);
    }
//
//    @Override
//    public void updatePicA(final String pic) {
////        ApiCreator.getApi().uploadFile(createFileBody(pic), createStrBody(DaoManager.getInstance().getUserEntity().getId() + ""), createStrBody("head"))
////                .compose(RxTransformer.<String>errorHandle())
////                .observeOn(AndroidSchedulers.mainThread())
////                .subscribe(new BaseObserver<String>() {
////                    @Override
////                    protected void onSuccess(String data) {
////                        mBaseIView.setPicA(data);
////                    }
////
////                    @Override
////                    public void onFailure(ResponeThrowable throwable) {
////                        super.onFailure(throwable);
////                    }
////                });
//    }
//
//    @Override
//    public void updatePicB(final String pic) {
////        ApiCreator.getApi().uploadFile2(createFileBody(pic))
////                .compose(RxTransformer.<String>errorHandle())
////                .observeOn(AndroidSchedulers.mainThread())
////                .subscribe(new BaseObserver<String>() {
////                    @Override
////                    protected void onSuccess(String data) {
////                        mBaseIView.setPicB(data);
////                    }
////
////                    @Override
////                    public void onFailure(ResponeThrowable throwable) {
////                        super.onFailure(throwable);
////                    }
////                });
//    }

    @Override
    public void upFile(String path, final int position) {
        ApiCreator.getApi().uploadPicture(createFileBody(path), createStrBody("1"))
                .compose(RxTransformer.<String>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onSuccess(String data) {
                        mBaseIView.getAdapter().getData().get(position).setUrl(data);
                        mBaseIView.getAdapter().notifyDataSetChanged();
                    }
                });
    }

    @Override
    public void submit(final int type) {
        if (checkData()) {
            addPic(type);
            mBaseIView.getContext().finish();
        }
    }

    /**
     * 检查是否有图片数据
     */
    private boolean checkData() {
        List<UpFileBean> data = mBaseIView.getAdapter().getData();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isMust()
                    && TextUtils.isEmpty(data.get(i).getUrl())) {
                ToastUtil.showToast("请上传" + data.get(i).getDownStr());
                return false;
            }
        }
        return true;
    }

    /**
     * 传递图片数据给上一级页面
     */
    private void addPic(final int type) {
        List<UpFileBean> beans = mBaseIView.getAdapter().getData();
        List<String> picList = new ArrayList<>();
        for (int i = 0; i < beans.size(); i++) {
            if (!TextUtils.isEmpty(beans.get(i).getUrl())) {
                picList.add(beans.get(i).getUrl());
            }
        }
        String data = GsonUtil.toJson(picList);
//        if (type == PicEnum.idCardPic.getId()) {
//            picBean.setIdCardPic(data);
//        } else
        if (type == PicEnum.householdRegister.getId()) {
            picBean.setHouseholdRegister(data);
        } else if (type == PicEnum.roomCert.getId()) {
            picBean.setRoomCert(data);
        } else if (type == PicEnum.businessCert.getId()) {
            picBean.setBusinessCert(data);
        } else if (type == PicEnum.otherHouseCert.getId()) {
            picBean.setOtherHouseCert(data);
        } else if (type == PicEnum.carCert.getId()) {
            picBean.setCarCert(data);
        } else if (type == PicEnum.carWarranty.getId()) {
            picBean.setCarWarranty(data);
        } else if (type == PicEnum.maritalCert.getId()) {
            picBean.setMaritalCert(data);
        } else if (type == PicEnum.creditCert.getId()) {
            picBean.setCreditCert(data);
        } else if (type == PicEnum.supplement.getId()) {
            picBean.setSupplement(data);
        }else if (type == PicEnum.newCreditCert.getId()) {
            picBean.setCreditCert(data);
        }else if (type == PicEnum.newBusinessCert.getId()) {
            picBean.setBusinessCert(data);
        }else if (type == PicEnum.newWarranty.getId()) {
            picBean.setCarWarranty(data);
        }
        picBean.setType(type);
        RxBus.get().send(RxBusCode.FILE_UPLOAD_SUCCESS, picBean);
    }

    private MultipartBody.Part createFileBody(final String pic) {
        File file = new File(pic);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        return MultipartBody.Part.createFormData("file", file.getName(), requestBody);
    }

    private RequestBody createStrBody(final String value) {
        return RequestBody.create(MediaType.parse("text/plain"), value);
    }

}
